<template>
	<view class="login">
		<u-navbar :is-back='false' title="登录" :background="{ background: '#6EC9E1' }" :border-bottom='false'
			:fixed="true" :title-bold='true' :placeholder="true" title-color="white">
		</u-navbar>


		<view class="selction">
			<image style="max-width: 160px;" src="../../static/img/title.png" mode="widthFix"></image>
			<h1>回访专业平台</h1>
			<view class="s-t2">
				<span>专业人士在线</span>
				<span>业余咨询</span>
				<span>专业回访</span>
				<span>定时解答</span>
			</view>
		</view>

		<u-toast ref="uToast" />
		<view class="wx">
			<button open-type="getPhoneNumber" @getphonenumber="wx">手机号快捷登录</button>
		</view>


		<view class="auth">
			<button @click="isSure=!isSure" open-type="agreePrivacyAuthorization">
				<view :style="isSure?'background-color: #aaffff':''" style="border-radius: 50%;height: 8px;width: 8px;">
				</view>
			</button>
			<span style="margin-left: 20rpx;">我同意3次回访平台</span>
			<span class="span click u-reset-button" @click="open(1)">《会员协议》</span>
			<span class="span click u-reset-button" @click="open(2)">《隐私协议》</span>
		</view>

		<!-- 返回首页 -->
		<view class="u-m-t-80 u-flex u-row-center">
			<span @click="back">暂不登录，返回首页</span>
		</view>

		<!--协议弹窗  -->
		<u-popup v-model="show" mode="center" :closeable="true" :border-radius='20'>
			<view class="agreement">
				<p>{{agreement.title}}</p>
				<scroll-view class="content" scroll-y="true">
					<rich-text :nodes="agreement.content"></rich-text>
				</scroll-view>
				<view class="event">
					<button @click="isSure=false,show=false">不同意</button>
					<button class="sure" @click="isSure=true,show=false"
						open-type="agreePrivacyAuthorization">同意</button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		login,
		sendCode,
		userAgreement,
		privacyAgreement
	} from '@/api/login/login.js'
	export default {
		data() {
			return {
				sid: '',
				isSure: false,
				show: false,
				agreement: ''
			}
		},
		onLoad() {
			this.sid = this.ws.sid;
			console.log(this.sid, 'login');
		},
		methods: {
			open(e) {
				if (e == 1) {
					userAgreement().then(res => {
						if (res.code == 1) {
							this.agreement = res.data.detail
							this.show = true
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'error'
							})
						}
					})
				} else {
					privacyAgreement().then(res => {
						if (res.code == 1) {
							this.agreement = res.data.detail
							this.show = true
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'error'
							})
						}
					})
				}
			},

			back() {
				uni.switchTab({
					url: '/pages/message/message'
				})
			},

			// 注册
			wx(e) {
				const that = this
				if (!that.isSure) {
					that.$refs.uToast.show({
						title: '请先阅读同意隐私政策！'
					})
					return;
				}
				uni.showLoading({
				  title: '授权中...',
				  mask: false, // 是否显示透明蒙层，防止触摸穿透
				});
				if (e.detail.encryptedData) {
					wx.login({
						success(res) {
							uni.hideLoading();
							if (res.code) {
								//发起网络请求
								let obj = {
									'encrypted_data': e.detail.encryptedData,
									'iv': e.detail.iv,
									'code': e.detail.code,
									'wx_code': res.code,
									'sid': that.sid
								}

								login(obj).then(data => {
									if (data.code == 1) {
										let user = data.data.userinfo
										that.ws.setUserInfo(user)
										that.ws.pageRefresh.message = true
										that.ws.pageRefresh.sessionInfo = true
										that.ws.init(user.token, user.auth_token)
										if (user.job == 0) {
											uni.redirectTo({
												url: '/sub/center/choose-role'
											})
										} else {
											// if (user.audit == 1 || user.audit > 2) {
											uni.switchTab({
												url: '/pages/message/message'
											})
											// } else {
											// 	uni.redirectTo({
											// 		url: '/sub/center/realnamenext'
											// 	})
											// }
										}
									} else {
										that.$refs.uToast.show({
											title: data.msg
										})
									}
								})

							} else {
								that.$refs.uToast.show({
									title: '授权失败,请重试！'
								})
								uni.hideLoading();
								console.log('微信code请求失败！' + res.errMsg)
							}
						}
					})
				} else {
					uni.showModal({
						title: '授权失败',
						content: '是否回到首页',
						success: function(res) {
							if (res.confirm) {
								uni.switchTab({
									url: '/pages/message/message'
								})

							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
					uni.hideLoading();
				}

			},

		}
	}
</script>

<style lang="less">
	.login {
		height: 100vh;
		background-color: #6EC9E1;
		color: white;
		overflow: hidden;

		position: relative;

		// ---------
		.agreement {
			max-height: 60vh;
			width: 90vw;
			min-height: 40vh;
			color: #474747;
			padding: 30rpx;
			padding-bottom: 80px;
			font-size:
				14px;

			p {
				text-align: center;
				margin: 20px 0;
				font-weight: bold;
				font-size: 18px;
			}

			.content {
				max-height: 40vh;

			}

			.event {
				display: flex;
				justify-content: space-between;
				width: 100%;
				position: absolute;
				bottom: 20px;
				left: 0;

				button {
					width: 140px;
					height: 50px;
					border-radius: 226px;
					background: #F4F4F4;
					box-sizing: border-box;
					border: #F4F4F4 !important;
				}

				.sure {
					background: #6EC9E1;
					border: #6EC9E1 !important;
					color: #FFF;
				}
			}
		}

		.selction {
			display: flex;
			flex-flow: column;
			align-items: center;
			font-size: 20rpx;
			margin-top: 140rpx;

			p {
				font-weight: bold;
				font-size: 60rpx;
			}

			h1 {
				margin: 20rpx 0;
				font-size: 32rpx;
			}

			.s-t2 {
				span {
					padding: 0 20rpx;
					border-left: 1rpx solid #FFF;
				}

				span:first-child {
					border-left: unset !important;
				}
			}

		}

		// ---------
		.form {
			margin-top: 40rpx;
			height: 600rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			flex-flow: column;
			padding: 0 40rpx;

			p {
				color: #ff1e0a;
				font-size: 14px;
			}

			h1 {
				margin: 10px 0;
			}

			.form-item {
				width: 100%;
				padding: 10rpx;
				color: black;
				margin: 20rpx;
				background-color: #FFF;
				display: flex;
				align-items: center;
				border-radius: 8rpx;

				._img {
					width: 18px;
				}

				input {
					line-height: 88rpx;
					height: 88rpx;
					padding: 10rpx;
					margin: 0 10rpx;
					flex: 3;
				}

				.getCode {
					color: #6EC9E1;
					// font-size: 24rpx;
					margin-right: 20rpx;
				}

				.plr-c {
					opacity: 0.5;
				}
			}

			.submit {
				margin-top: 60rpx;
				background-color: #20ACD1;
				text-align: center;

				.plr-c {
					color: #FFF;
					opacity: 1;
				}
			}

			.form-item:last-child {
				background-color: unset;
				font-size: 24rpx;
				padding: unset;
				color: #20ACD1;
			}
		}

		// --------
		.wx {
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			background-color: #20ACD1;
			border-radius: 179px;
			width: 90vw;
			height: 60px;
			margin: 20px auto;
			margin-top: 100px;
			overflow: hidden;

			button {
				width: 100%;
				height: 100%;
				display: flex;
				justify-content: center;
				align-items: center;
				padding: unset;
				background: unset;
				border: unset;
				color: #FFF;
				font-size: 16px;
			}

		}

		.auth {
			display: flex;
			align-items: center;
			font-size: 12px;
			justify-content: center;

			.span {
				color: #20ACD1;
			}

			span {
				opacity: 0.6;
			}

			button {
				padding: 0;
				border-radius: 50%;
				display: flex;
				justify-content: center;
				align-items: center;
				margin: 0;
				width: 16px;
				height: 16px;
				border: #FFF solid 2px;
			}
		}
	}
</style>